/*<script src="ajax_json.js" type="text/javascript"></script>*/

function add_cursus(user_id){
level = document.getElementById("level").value;	
etablissement = document.getElementById("etablissement").value;	
year= document.getElementById("year").value;	
description = document.getElementById("description").value;	

var requette = new AjaxRequest("../members/query.php?op=add_cursus&user_id=" + user_id + "&level=" + level + "&etablissement=" + etablissement+ "&year=" + year + "&description=" + description ,"add_cursus_parse");
	show_cursus(user_id);		
}

function add_cursus_parse(_json){
		if(_json["res"] == "yes"){
	    	msg = "<font id='safe_note'> Les Information ont bien ajoute </font>";
	    }
	    else
	    msg = " Eureur en Ajouter "; 
		document.getElementById("cursus_res").innerHTML = msg;
}

//--------------

function update_per_info(user_id){
	lname = document.getElementById("lname").value;
	fname = document.getElementById("fname").value;
	photo = document.getElementById("fileToUpload").value;
	tel   = document.getElementById("tel").value;
	gendre   = document.getElementById("gendre").value;
	var requette = new AjaxRequest("query.php?op=info&user_id="+ user_id +"&lname=" + lname + "&fname=" + fname+ "&photo=" + photo + "&tel=" + tel+ "&gendre=" + gendre,"parse_update_per");		
	}
	
function parse_update_per(_json) {
	    if(_json["res"] == "yes"){
	    	msg = "<font id='safe_note'> Les Information ont bien modifies </font>";
	    }
	    else
	        msg = "<font id='error_note'> Eureur en Modification </font>"; 
		document.getElementById("result").innerHTML = msg;
//	    show_cursus(_json[0]["id_user_fkn"]);
		
}

//--------------

function show_cursus(user_id){
	var requette = new AjaxRequest("query.php?op=show_cursus&user_id="+user_id,"parser_show_cur");
}

function parser_show_cur(_json){
	if (_json.length == 1) {
			var _html = '<div id="compteur">' + _json.length + ' trouv&eacute;</div>';
		} else {
			var _html = '<div id="compteur">' + _json.length + ' trouv&eacute;s</div>';
		}
		_html += "<table id='tab_info'>";
		_html += "<tr> <th> Niveau </th> <th> Etablissement </th> <th> Annee </th> <th> Description </th> <th> Supprimer </th> </tr>"; 
		for(var i=0; i < _json.length; i++) {
			
			_html += '<tr>';
			_html += '<td width="90px">' + _json[i]["level"] + ' </td><td width="90px"> ' + _json[i]["etablissement"] + ' </td><td width="90px"> ' +_json[i]["year"] + ' </td><td width="340px"> ' +_json[i]["description"] + ' </td><td width="90px"> <a onclick="delete_cursus('+_json[i]["id_cursus"]+',' + _json[i]["id_user_fkn"] + ')"> <img src="../images/del.gif"> </a></td>';
			_html += '</tr>';
					
		}
		_html += "</table>";
		
		document.getElementById("show_cursus").innerHTML = _html;
}
//--------------

function delete_cursus(cursus_id,id_user){
	if(confirmDelete())
        var requette = new AjaxRequest("query.php?op=delete_cursus&cursus_id="+ cursus_id+"&id_user="+id_user , "parser_delete_cur");	
}

function parser_delete_cur(_json){
	//document.getElementById("show_cursus").innerHTML = "";
	show_cursus(_json["user_id"]);
	
}

//--------------

//----------EXPERIENCE
function show_exp(user_id){
	var requette = new AjaxRequest("query.php?op=show_experience&user_id="+user_id,"parser_show_exp");
}

function parser_show_exp(_json){
	if (_json.length == 1) {
			var _html = '<div id="compteur">' + _json.length + ' trouv&eacute;</div>';
		} else {
			var _html = '<div id="compteur">' + _json.length + ' trouv&eacute;s</div>';
		}
		
	_html += '<table id="tab_info">'; 
	_html += "<tr> <th> Poste </th> <th> Company </th> <th> Annee </th> <th> Responsibilites </th> <th> Supprimer </th> </tr>"; 
		for(var i=0; i < _json.length; i++) {
			_html += '<tr>';
			_html += '<td width="250px">' + _json[i]["post"] + ' </td><td width="100px"> ' + _json[i]["company"] + ' </td><td width="100px"> ' +_json[i]["year"] + ' </td><td width="250px"> ' +_json[i]["res"] + ' </td><td width="100px"> <a onclick="delete_exp('+_json[i]["id_exp"]+', '+ _json[i]["id_user_fkn"] +')"> <img src="../images/del.gif"> </a></td>';
			_html += '</tr>';
					
		}
		_html += '</table>';
		
		document.getElementById("show_exp").innerHTML = _html;
}

function add_exp(user_id){
post = document.getElementById("post").value;	
company = document.getElementById("company").value;	
year= document.getElementById("year_exp").value;	
resp = document.getElementById("resp").value;	

var requette = new AjaxRequest("../members/query.php?op=add_exp&user_id=" + user_id + "&post=" + post + "&company=" + company+ "&year=" + year + "&resp=" + resp ,"add_exp_parse");
show_exp(user_id);		
}

function add_exp_parse(_json){
		if(_json["res"] == "yes"){
	    	msg = "<font id='safe_note'> Les Information ont bien ajoute </font>";
	    }
	    else
	    msg = " Eureur en Ajouter "; 
		document.getElementById("exp_res").innerHTML = msg;
}


function delete_exp(exp_id,id_user){
	if(confirmDelete())
        var requette = new AjaxRequest("query.php?op=delete_exp&exp_id="+ exp_id+"&id_user="+id_user , "delete_exp_parser");	
}

function delete_exp_parser(_json){
	//document.getElementById("show_cursus").innerHTML = "";
	show_exp(_json["user_id"]);
	
}



//-------------------------------------
//------------------------------------- ALBUMS FUNCTINO 

function show_albums(user_id){
	var requette = new AjaxRequest("query.php?op=show_album&user_id="+user_id,"parser_show_album");
}

function parser_show_album(_json){
	if (_json.length == 1) {
			var _html = '<div id="compteur">' + _json.length + ' trouv&eacute;</div> ';
		} else {
			var _html = '<div id="compteur">' + _json.length + ' trouv&eacute;s</div>';
		}
		
		_html += '<table id="tab_info">';
		_html += '<tr> <th width="250px"> Nom d\'album </th>';
		_html += '<th width="150px"> Acceder </th>';
		_html += '<th width="100px"> Supprimer </th>';
		_html += '<th width="100px"> Ajouter </th>';
		_html += '<th width="100px"> Visualiser </th>';
		
		
		for(var i=0; i < _json.length; i++) {
			
			_html += '<div>';
			_html += '<table id="tab_info">';
			_html += '<tr id="tr_result_row">';
			_html += '<td width="250px;"><a onclick="display_photo_div('+_json[i]['id_album']+')">' + _json[i]["album"] + ' </a></td>';
			_html += '<td width="150px;"> ' + _json[i]["is_pub"] +' </td>';
			_html += '<td width="100px;"><a onclick="delete_album('+_json[i]["id_album"]+', '+ _json[i]["id_user_fkn"] +')"> <img src="../images/delete_photo.gif" width="30px"> </a></td>';
			_html += '<td width="100px;"> <a onclick="add_photos('+_json[i]["id_album"]+');"><img src="../images/add_photo.gif" width="30px"></a> </td>'; 
			_html += '<td width="100px;"><a href="flash/show_album.php?album_id='+_json[i]["id_album"]+'&user_id='+_json[i]["id_user_fkn"]+'" target="_blank"> <img src="../images/show_photo.gif" width="30px"></a></td>';
			_html += '</tr></table></div>';
			_html += '<div id='+ _json[i]["id_album"] +'> </div>';
			_html += '<div id=photo_'+ _json[i]["id_album"] +'> </div>';
			_html += '</div>';
					
		}
		
		document.getElementById("show_albums").innerHTML = _html;
}

function add_album(user_id){
album = document.getElementById("album").value;
if(document.getElementById("public").checked)
  is_pub = 1;
else
  is_pub = 0;

var requette = new AjaxRequest("../members/query.php?op=add_album&user_id=" + user_id + "&album=" + album + "&is_pub=" + is_pub ,"add_album_parse");
show_albums(user_id);		
}

function add_album_parse(_json){
		if(_json["res"] == "yes"){
	    	msg = "<font id='safe_note'> Les Information ont bien ajoute </font>";
	    }
	    else
	    msg = " Eureur en Ajouter "; 
		document.getElementById("album_res").innerHTML = msg;
}


function delete_album(album_id,id_user){
	  if(confirmDelete())
        var requette = new AjaxRequest("query.php?op=delete_album&album_id="+ album_id+"&id_user="+id_user , "delete_album_parser");	
}

function delete_album_parser(_json){
	show_albums(_json["user_id"]);
	
}

function get_photos(album_id){
	var requette = new AjaxRequest("../members/query.php?op=get_photos&album_id=" + album_id ,"get_photos_parse");
}

function get_photos_parse(_json){	
	   if(_json[0]["id_photo"] == "nophoto"){
	        _html = "<i> Pas de photos </i> ";
	        div_id = _json[0]["id_album"];
	   }
	   else{
	   		div_id = _json[0]["id_album"];
	   	    _html = "<div>"; 
			for(var i=0; i < _json.length; i++) {
			_html += ' <img src="../photos/thumbnail/thumb_'+_json[i]["path"]+'" onclick="show_photo_detail('+div_id+','+_json[i]["id_photo"]+');">';
		    }
		    _html += ' </div>';
	   }
	    
	   document.getElementById(div_id).innerHTML = _html;
}

function display_photo_div(album_id){
	if(document.getElementById(album_id).innerHTML != ""){
		document.getElementById(album_id).innerHTML = "";
		div_photo = "photo_"+album_id;
		document.getElementById(div_photo).innerHTML = "";
		//document.getElementById(album_id).style.display = "none";
	}else
	get_photos(album_id);
}

function add_photos(album_id){
	document.getElementById(album_id).innerHTML = "";
	window.open('../add_photos.php?album_id='+album_id,'mywindow','width=400,height=400,left=100,top=100');
//	window.open('../add_photos.php'); 
}

//when we click on the photo
function show_photo_detail(div_id,photo_id){
	id_div      = "photo_"+div_id;
	if(document.getElementById(id_div).innerHTML == "")
		requette = new AjaxRequest("../members/query.php?div_id="+div_id+"&op=show_photo_detail&photo_id="+photo_id,"show_photo_detail_parse");
	else
	   document.getElementById(id_div).innerHTML = "";
}

function show_photo_detail_parse(_json){
	id_photo    = _json["id_photo"];
	id_album    = _json["id_div"];
	id_div      = "photo_"+_json["id_div"];
	path        = _json["path"];
	title       = _json["title"]; 
	description = _json["description"]; 
	_html       = "<div style='width:600px; text-align:cneter'>";
	//_html      += "<img src='../photos/normal/normal_"+path+"'><br>";
	_html      += "Title: <input type='text' id='photo_"+id_photo+"' value='"+title+"'> <br>";
	_html      += "Description: <input type='text' id='desc_"+id_photo+"' value='"+description+"'> <br>";
	_html      += "<input type='button' value='Modifier'  onclick='modify_photo("+id_photo+");'>";
	_html      += "<input type='button' value='Supprimer' onclick='delete_photo("+id_photo+","+id_album+");'>"; 
	document.getElementById(id_div).innerHTML = _html; 	
}

function modify_photo(id_photo){
	title = "photo_"+id_photo;
	desc  = "desc_"+id_photo;
	title = document.getElementById(title).value;
	desc  = document.getElementById(desc).value;
	requette = new AjaxRequest("../members/query.php?op=modify_photo&id_photo="+id_photo+"&title="+title+"&desc="+desc,"parse_modify_photo");
}

function parse_modify_photo(_json){
	
}

function delete_photo(id_photo,id_album){
  if(confirmDelete())
	requette = new AjaxRequest("../members/query.php?op=delete_photo&id_photo="+id_photo+"&id_album="+id_album,"parse_delete_photo");
}

function parse_delete_photo(_json){
	album_id = _json["album_id"];
	photo_div = "photo_"+album_id;
	document.getElementById(photo_div).innerHTML = "";
	get_photos(album_id);	
}

function confirmDelete() {
  if (confirm("Voulez vous vraiment confirmer la suppression? ")) {
    return true;
  }
  else
  return false;
}

//------------------------------------- 
